package com.wuyan.warriorsoffate.ui.page

import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.*
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsFocusedAsState
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material.icons.filled.Edit
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.PopupProperties
import androidx.navigation.NavController
import com.wuyan.warriorsoffate.R
import com.wuyan.warriorsoffate.data.*
import com.wuyan.warriorsoffate.ui.theme.WeContentColorFor
import com.wuyan.warriorsoffate.ui.theme.WeTheme
import com.wuyan.warriorsoffate.ui.view.*
import kotlinx.coroutines.launch

@Composable
fun dropdownMenuItemTest(state:MutableState<Boolean>,text:String,onClick:()->Unit){
    val interactionSource = remember { MutableInteractionSource() }
    val pressState = interactionSource.collectIsPressedAsState()
    val focusState = interactionSource.collectIsFocusedAsState()
    DropdownMenuItem(
        onClick = {
            state.value = false
            onClick()
        },
        enabled = true,
        interactionSource = interactionSource
    ) {
        Text(text = text,modifier = Modifier.padding(start = 10.dp),color = if(pressState.value || focusState.value) Color.Red else Color.Black)
    }
}

//主框架
@ExperimentalAnimationApi
@Composable
fun HomePage(nes:NesViewModel, navController: NavController){
    val expandState = remember {
        mutableStateOf(false)
    }
    WeSurface(modifier = Modifier.fillMaxSize()) {
        Column(modifier = Modifier.fillMaxSize()) {
            Box(modifier = Modifier
                .fillMaxWidth()
                .wrapContentHeight()) {
                Row(modifier =Modifier
                    .align(Alignment.CenterStart)) {
                    WeButton(text = if (nes.state.isHex.value) "HEX" else "DEC",
                        modifier = Modifier
                            .padding(WeTheme.weDp.paddingSmall), onClick = {
                            nes.state.isHex.value = !nes.state.isHex.value
                        })
                    WeButton(text = "更多",
                        modifier = Modifier
                            .padding(WeTheme.weDp.paddingSmall), onClick = {
                            expandState.value=true
                        })
                    DropdownMenu(expanded = expandState.value, onDismissRequest = {
                        expandState.value=false
                    }, offset = DpOffset(10.dp,10.dp),
                        properties = PopupProperties()
                    ) {
                        dropdownMenuItemTest(expandState,"打开"){
                            Config.state.value=SelectFileDialogType.Open
                        }
                        dropdownMenuItemTest(expandState,"另存"){
                            Config.state.value=SelectFileDialogType.SaveAs
                        }
                        dropdownMenuItemTest(expandState,"重置"){
                            Config.state.value=SelectFileDialogType.Reset
                        }
                    }
                    SelectFileDialog(Config.state, nes = nes)
                }
                WeText(text = nes.state.displayPage.toString(),
                    textAlign = TextAlign.Center,
                    modifier = Modifier.align(Alignment.Center))
                Icon(painter = painterResource(id = R.drawable.theme), contentDescription = "显示风格", tint = WeTheme.colors.text, modifier = Modifier
                    .size(40.dp)
                    .align(Alignment.CenterEnd)
                    .clickable {
                        nes.nextTheme()
                    })
            }
            Box(modifier = Modifier.weight(1.0f))  {
                when(nes.state.displayPage){
                    DisplayMainPage.PersonList-> PersonMainDisplay(nes,navController)
                    DisplayMainPage.PropList->PropsDisplay(nes = nes, modifier = Modifier
                        .padding(WeTheme.weDp.marginMedium)
                        .fillMaxSize())
                    DisplayMainPage.WeaponList->WeaponDisplay(nes = nes, modifier = Modifier
                        .padding(WeTheme.weDp.marginMedium)
                        .fillMaxSize())
                    DisplayMainPage.DefenseList->DefenseDisplay(nes = nes, modifier = Modifier
                        .padding(WeTheme.weDp.marginMedium)
                        .fillMaxSize())
                    DisplayMainPage.SundryList-> SundryPage(nes = nes, modifier = Modifier
                        .fillMaxSize())
                }
            }
            WeSurface(
                Modifier
                    .shadow(WeTheme.weDp.paddingLarge)
                    .fillMaxWidth()
                    .wrapContentHeight()) {
                Row(modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                    verticalAlignment = Alignment.CenterVertically) {
                    for (page in DisplayMainPage.values()){
                        val color=if (page==nes.state.displayPage) WeTheme.colors.label1 else WeTheme.colors.text
                        WeText(text = page.toString(),modifier = Modifier
                            .weight(1.0f)
                            .clickable {
                                nes.state.displayPage = page
                            }
                            .padding(
                                top = WeTheme.weDp.paddingLarge,
                                bottom = WeTheme.weDp.paddingLarge
                            ),
                            color = color, textAlign = TextAlign.Center)
                    }
                }
            }

        }
    }
}
//显示武将简略信息列表
@ExperimentalAnimationApi
@Composable
fun PersonMainDisplay(nes:NesViewModel,navController: NavController,modifier: Modifier=Modifier){
    val ids: SnapshotStateList<Int> = remember {
        mutableStateListOf()
    }
    val scope= rememberCoroutineScope()
    Column(modifier) {
        WeSearchHasList(data = ids, onSearch = {value->
            val _ids=nes.searchPerson(value)
            if (_ids.size>0){
                ids.clear()
                for (i in _ids){
                    ids.add(i)
                }
                scope.launch {
                    nes.state.personListState.scrollToItem(ids[0])
                }
            }
        }, onSelect = {
            scope.launch {
                nes.state.personListState.scrollToItem(ids[it])
            }
        }, modifier = Modifier.padding(WeTheme.weDp.marginMedium))
        WeColumn(state = nes.state.personListState, modifier = Modifier
            .padding(WeTheme.weDp.marginMedium)
            .wrapContentSize()){
            items(nes.persons.size){index->
                WeSurface(modifier = Modifier.clickable {
                    navController.navigate("${DisplayPage.PersonInfo.name}/$index")
                }) {
                    Row(modifier = Modifier
                        .padding(WeTheme.weDp.marginSmall)
                        .fillMaxWidth()
                        .wrapContentHeight()
                        .shadow(
                            WeTheme.weDp.marginSmall / 2,
                            shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                        )
                        .background(
                            WeTheme.colors.bg,
                            shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                        )
                        .padding(WeTheme.weDp.paddingSmall),
                        verticalAlignment = Alignment.CenterVertically,
                        horizontalArrangement = Arrangement.SpaceBetween) {
                        WeLabel(text = (index+1).toString(if (nes.state.isHex.value) 16 else 10), bgColor = WeTheme.colors.label1)
                        WeLabel(text = nes.persons[index].name, bgColor = WeTheme.colors.label2)
                        WeLabel(text = "武力："+ nes.persons[index].force.toString(if (nes.state.isHex.value) 16 else 10), bgColor = WeTheme.colors.text)
                        WeLabel(text = "智力："+ nes.persons[index].brains.toString(if (nes.state.isHex.value) 16 else 10), bgColor = WeTheme.colors.label1)
                    }
                }
            }

        }
    }
}
//显示道具列表
@ExperimentalAnimationApi
@Composable
fun PropsDisplay(nes:NesViewModel, modifier: Modifier=Modifier){
    WeColumn(state = nes.state.propsListState, modifier = modifier) {
       items(nes.props.size){i->
           val prop=nes.props[i]
           Row(modifier = Modifier
               .padding(WeTheme.weDp.marginSmall)
               .fillMaxWidth()
               .wrapContentHeight()
               .shadow(
                   WeTheme.weDp.marginSmall / 2,
                   shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
               )
               .background(
                   WeTheme.colors.bg,
                   shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
               )
               .padding(WeTheme.weDp.paddingSmall),
               verticalAlignment = Alignment.CenterVertically,
               horizontalArrangement = Arrangement.SpaceBetween) {
               WeLabel(text = (i+1).toString(if (nes.state.isHex.value) 16 else 10), bgColor = WeTheme.colors.label1)
               WeLabel(text = prop.name,bgColor= WeTheme.colors.label2)
               WeLabel(bgColor= WeTheme.colors.text){
                   WeText(text = "价格：", color = WeTheme.colors.onText)
                   if (nes.state.isHex.value){
                       TextFieldHex65535(dec = prop.money, change = {
                           prop.money=it
                       })
                   }else{
                       TextFieldDec65535(dec = prop.money, change = {
                           prop.money=it
                       })
                   }

               }
           }
       }
    }
}
//显示武器列表
@ExperimentalAnimationApi
@Composable
fun WeaponDisplay(nes:NesViewModel, modifier: Modifier=Modifier){
    WeColumn(state = nes.state.weaponListState, modifier = modifier) {
        items(nes.weapon.size){i->
            val weapon=nes.weapon[i]
            Row(modifier = Modifier
                .padding(WeTheme.weDp.marginSmall)
                .fillMaxWidth()
                .wrapContentHeight()
                .shadow(
                    WeTheme.weDp.marginSmall / 2,
                    shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                )
                .background(
                    WeTheme.colors.bg,
                    shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                )
                .padding(WeTheme.weDp.paddingSmall),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween) {
                WeLabel(text = (i+1).toString(if (nes.state.isHex.value) 16 else 10))
                WeLabel(text = weapon.name,bgColor= WeTheme.colors.label2)
                DisTitleLabel16(title = "价格:", dec = weapon.money, change = {
                    weapon.money=it
                },nes.state.isHex)
                DisTitleLabel8(title = "显伤:", dec = weapon.displayAttack, change = {
                    weapon.displayAttack=it
                },nes.state.isHex)
                DisTitleLabel16(title = "真伤:", dec = weapon.trueAttack, change = {
                    weapon.trueAttack=it
                },nes.state.isHex)
            }
        }
    }
}
@Composable
fun DisTitleLabel16(title:String,dec:Int,change :(Int)->Unit,isHex:MutableState<Boolean>,bgColor:Color=WeTheme.colors.text){
    WeLabel(bgColor = bgColor){
        Row() {
            WeText(text = title,color= WeContentColorFor(backgroundColor = bgColor))
            if (isHex.value){
                TextFieldHex65535(dec = dec, change = change)
            }else{
                TextFieldDec65535(dec = dec, change = change)
            }
        }
    }
}
@Composable
fun DisTitleLabel8(title:String,dec:Int,change :(Int)->Unit,isHex:MutableState<Boolean>,bgColor:Color=WeTheme.colors.text){
    WeLabel(bgColor = bgColor){
        Row() {
            WeText(text = title,color= WeContentColorFor(backgroundColor = bgColor))
            if (isHex.value){
                TextFieldHex255(dec = dec, change = change)
            }else{
                TextFieldDec255(dec = dec, change = change)
            }
        }
    }
}

//显示武器列表
@ExperimentalAnimationApi
@Composable
fun DefenseDisplay(nes:NesViewModel, modifier: Modifier=Modifier){
    WeColumn(state = nes.state.defenseListState, modifier = modifier) {
        items(nes.defense.size){i->
            val defense=nes.defense[i]
            Row(modifier = Modifier
                .padding(WeTheme.weDp.marginSmall)
                .fillMaxWidth()
                .wrapContentHeight()
                .shadow(
                    WeTheme.weDp.marginSmall / 2,
                    shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                )
                .background(
                    WeTheme.colors.bg,
                    shape = RoundedCornerShape(WeTheme.weDp.marginSmall)
                )
                .padding(WeTheme.weDp.paddingSmall),
                verticalAlignment = Alignment.CenterVertically,
                horizontalArrangement = Arrangement.SpaceBetween) {
                WeLabel(text = (i+1).toString(if (nes.state.isHex.value) 16 else 10))
                WeLabel(text = defense.name,bgColor= WeTheme.colors.label2)
                DisTitleLabel16(title = "价格:", dec = defense.money, change = {
                    defense.money=it
                },nes.state.isHex)
                DisTitleLabel8(title = "防御:", dec = defense.defense, change = {
                    defense.defense=it
                },nes.state.isHex)
            }
        }
    }
}

